/**
 * webpack配置文件
 */
const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const EslintPlugin = require('eslint-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;


module.exports = {
  // 打包环境设置
  mode: 'development',

  // 入口
  entry: {
    index: './src/index.js',
    mine: './src/mine.js',
  },

  // 出口
  output: {
    path: resolve(__dirname, 'output'),
    filename: '[name].bundle.js',
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
    },
  },

  // 模块配置
  module: {
    rules: [
      // 配置多个规则
      // 解析css
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
      },

      // 解析less
      {
        test: /\.less$/i,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'less-loader',
        ],
      },

      // 编辑es5以上的语法
      {
        test: /\.m?js$/i,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              [
                '@babel/preset-env',
                {
                  useBuiltIns: 'usage', // 按需加载
                  corejs: 3,
                  targets: 'defaults',
                },
              ],
            ],
          },
        },
      },

      // 解析打包图片
      {
        test: /\.(png|gif|jpe?g)$/i,
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 13 * 1024,
          },
        },
        generator: {
          filename: './images/[name][ext]',
        },
      },

      // 解析打包字体
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/i,
        // asset可以在asset/inline和asset/resource之间进行切换，文件小于8kb时使用asset/inline，否则使用asset/resource
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 8 * 1024,
          },
        },
        generator: {
          filename: './fonts/[name][ext]',
        },
      },
    ],
  },

  // 开发服务器
  devServer: {
    static: {
      directory: resolve(__dirname, 'output'),
    },
    liveReload: true,
    compress: true,
    port: 8888,
    proxy: {
      '/api': {
        target: 'https://api.xdclass.net',
        pathRewrite: {
          '^/api': '',
        },
        secure: false,
      },
      changeOrigin: true,
    },
  },

  target: 'web',

  // 插件
  plugins: [
    // 自动生成HTML文件和指定的模板
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
      title: '首页',
      chunks: ['index'],
    }),
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'mine.html',
      title: '我的',
      chunks: ['mine'],
    }),

    // 抽离css代码生成独立的文件
    new MiniCssExtractPlugin({
      filename: './css/main.css',
    }),

    // 压缩css
    new OptimizeCssAssetsPlugin(),
    new BundleAnalyzerPlugin(),
  ],
};
